using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Samples.WebControls
{
    /// <summary>
    /// This control is responsable for 
    /// producing the HTML that will 
    /// embed the multimedia
    /// files on the page
    /// </summary>
    public class MultimediaWebControl : BaseFieldControl
    {
        public MultimediaWebControl()
            : base()
        {
        }

        /// <summary>
        /// Returns a string that is 
        /// used to locate a ASCX template file
        /// </summary>
        protected override string DefaultTemplateName
        {
            get
            {
                return "MultimediaField";
            }
        }


        protected TextBox urltextbox;
        protected TextBox widthtextbox;
        protected TextBox heighttextbox;

        protected MultimediaFieldValue fieldValue = new MultimediaFieldValue();

        /// <summary>
        /// The Render method has been 
        /// overloaded to provide apropiate rendering 
        /// for each of the supported multimedia files.
        /// </summary>
        /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
            if (base.ControlMode == SPControlMode.Display)
            {
                fieldValue = (MultimediaFieldValue)this.ListItemFieldValue;
                if (fieldValue != null)
                {
                    string urlvalue = fieldValue.Url;
                    string width = fieldValue.Width;
                    string height = fieldValue.Height;
                    string extension = urlvalue.Substring(urlvalue.Length - 3);
                    if (extension.Equals("swf"))
                    {
                        output.WriteLine("<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " +
                        "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" " +
                        "WIDTH=" + width + " HEIGHT=" + height + "> " +
                        "<PARAM NAME=movie VALUE=\"" + urlvalue + "\"> " +
                        "<PARAM NAME=quality VALUE=high> " +
                        "<PARAM NAME=bgcolor VALUE=#000000> " +
                        "<EMBED src=\"" + urlvalue + "\" quality=high bgcolor=#000000 WIDTH=\"" + width + "\" HEIGHT=\"" + height + "\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></EMBED> " +
                        "</OBJECT>");
                    }
                    else if (extension.Equals("pdf"))
                    {
                        output.WriteLine("<EMBED src=\"" + urlvalue + "\" width=\"" + width + "\" height=\"" + height + "\" type=\"application/pdf\" > ");

                    }
                    else if (extension.Equals("wmv") || extension.Equals("avi") || extension.Equals("mpeg") || extension.Equals("mpg"))
                    {
                        output.WriteLine("<OBJECT ID=\"MediaPlayer\" WIDTH=\"" + width + "\" HEIGHT=\"" + height + "\" " +
                        "CLASSID=\"CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" " +
                        " STANDBY=\"Loading Windows Media Player components...\" " +
                        " TYPE=\"application/x-oleobject\"> " +
                        " <PARAM NAME=\"FileName\" VALUE=\"" + urlvalue + "\">" +
                        " <EMBED TYPE=\"application/x-mplayer2\" " +
                        " SRC=\"" + urlvalue + "\"" +
                        " NAME=\"MediaPlayer\" " +
                        " WIDTH=\"" + width + "\" " +
                        " HEIGHT=\"" + height + "\" > " +
                        "</EMBED> " +
                        " </OBJECT>");
                    }
                    else if (extension.Equals("mov"))
                    {
                        output.WriteLine(
                        "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" height=\"" + height + " width=\"" + width + "\">" +
                        "<param name=\"src\" value=\"" + urlvalue + "\">" +
                        "<param name=\"autoplay\" value=\"true\">" +
                        "<param name=\"type\" value=\"video/quicktime\" height=\"" + height + "\" width=\"" + width + "\">" +
                        "<embed src=\"" + urlvalue + "\" height=\"" + height + "\" width=\"" + width + "\" autoplay=\"true\" type=\"video/quicktime\" pluginspage=\"http://www.apple.com/quicktime/download/\"> " +
                        "</object>");
                    }
                }
            }
        }

        /// <summary>
        /// Gets the TextBoxes from the Template
        /// </summary>
        protected override void CreateChildControls()
        {
            if (Field == null)
                return;

            base.CreateChildControls();

            if (ControlMode == SPControlMode.Display)
                return;

            urltextbox = (TextBox)TemplateContainer.FindControl("multimediaurl");
            if (urltextbox == null)
                throw new ArgumentException("Corrupted " + DefaultTemplateName + " template - missing multimediaurl.");

            widthtextbox = (TextBox)TemplateContainer.FindControl("mutimediawidth");
            if (widthtextbox == null)
                throw new ArgumentException("Corrupted " + DefaultTemplateName + " template - missing mutimediawidth.");

            heighttextbox = (TextBox)TemplateContainer.FindControl("mutimediaheight");
            if (heighttextbox == null)
                throw new ArgumentException("Corrupted " + DefaultTemplateName + " template - missing mutimediaheight.");
        }

        /// <summary>
        /// Gets or Sets the MultimediaValue
        /// </summary>
        public override object Value
        {
            get
            {
                EnsureChildControls();
                fieldValue = new MultimediaFieldValue();
                fieldValue.Url = (urltextbox != null) ? urltextbox.Text : "value not set";
                fieldValue.Width = (widthtextbox != null) ? widthtextbox.Text : "value not set";
                fieldValue.Height = (heighttextbox != null) ? heighttextbox.Text : "value not set";
                return fieldValue;
            }
            set
            {
                EnsureChildControls();
                fieldValue = (MultimediaFieldValue)value;
                urltextbox.Text = fieldValue.Url;
                widthtextbox.Text = fieldValue.Width;
                heighttextbox.Text = fieldValue.Height;
            }
        }


        /// <summary>
        /// Makes sure that only links to 
        /// supported multimedia files are specified
        /// </summary>
        public override void Validate()
        {
            if (((base.ControlMode != SPControlMode.Display) && base.IsValid) && this.Visible)
            {
                base.Validate();
                MultimediaFieldValue value = (Value as MultimediaFieldValue);
                if (value != null)
                {
                    string url = value.Url.ToLower();
                    if (!url.EndsWith(".avi") && !url.EndsWith(".swf") && !url.EndsWith(".pdf") && !url.EndsWith(".mov") && !url.EndsWith(".mpeg") && !url.EndsWith(".mpg") && !url.EndsWith(".wmv"))
                    {
                        base.ErrorMessage = url + "**Multimedia file not supported. This controls supports QuickTime (.mov), Flash (.swf), MPEG videos (.mpeg,.mpg), Windows Media Videos (.avi or .wmv) and Acrobat Reader PDF (.pdf) files!";
                        base.IsValid = false;
                    }
                    else
                        base.IsValid = true;
                }

            }
        }
    }
}